#include "DiscussPane.h"
#include "ui_DiscussPane.h"
#include "LutController.h"

DiscussPane::DiscussPane(int gid, QWidget *parent) :
	QWidget(parent),
	ui(new Ui::DiscussPane),
	guyId(gid)
{
    ui->setupUi(this);
	ui->textBrowser->setOpenExternalLinks(true);

	connect(ui->lineEdit, SIGNAL(returnPressed()), ui->pushButton, SIGNAL(clicked()));
	connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(sendMsg()));
}

DiscussPane::~DiscussPane()
{
    delete ui;
}


void DiscussPane::displayInfo(QString str)
{
	ui->textBrowser->setTextColor(Qt::gray);
	ui->textBrowser->insertPlainText(str);
	moveToBottom();
}

void DiscussPane::clearPane()
{
	ui->textBrowser->clear();
	moveToBottom();
}

void DiscussPane::displayError(QString str)
{
	ui->textBrowser->setTextColor(Qt::red);
	ui->textBrowser->insertPlainText(str);
	moveToBottom();
}

void DiscussPane::displayMsg(int from, QString str)
{
	moveToBottom();


	if(LutController::getInstance()->getShowTime())
	{
		QTime ct = QTime::currentTime();


		ui->textBrowser->setFontWeight(QFont::Normal);
		ui->textBrowser->setTextColor(Qt::gray);
		ui->textBrowser->insertPlainText("("+ QString((ct.hour()<10) ? "0":"") + QString::number(ct.hour())
										 +":"+ ((ct.minute()<10) ? "0":"") + QString::number(ct.minute())
										 +":"+ ((ct.second()<10) ? "0":"") +QString::number(ct.second())+") ");
	}

	ui->textBrowser->setFontWeight(QFont::Bold);
	ui->textBrowser->setTextColor(Qt::darkBlue);
	if(from == LutController::getInstance()->myId())
		ui->textBrowser->setTextColor(Qt::darkRed);
	ui->textBrowser->insertPlainText(LutController::getInstance()->getNameFromId(from) + ": ");
	ui->textBrowser->setFontWeight(QFont::Normal);

	str.replace(":)", "<img src=\":/img/sourire.gif\" alt='' title=\":)\" />");
	str.replace(":(", "<img src=\":/img/triste.gif\" alt='' title=\":(\" />");
	str.replace(":D", "<img src=\":/img/D.gif\" alt='' title=\":D\" />");
	str.replace(":nerd:", "<img src=\":/img/nerd.gif\" alt='' title=\":nerd:\" />");
	str.replace(";)", "<img src=\":/img/clin_oeil.gif\" alt='' title=\";)\" />");
	str.replace(":p", "<img src=\":/img/p.gif\" alt='' title=\":p\" />");

	str.replace(QRegExp("<(?!(img.*>))"), "&lt;");
	str.replace(QRegExp("(https?://[^ ]*)"), "<a href='\\1'>\\1</a> ");
	str.replace("%E2%82%AC", QChar(8364));

	str.replace("\n", "<br />");

	ui->textBrowser->insertHtml(str);
	ui->textBrowser->setTextColor(Qt::black);
	ui->textBrowser->setFontUnderline(false);
	ui->textBrowser->insertPlainText("\n");
	moveToBottom();
	emit toRead(this);
}


bool DiscussPane::isPrivate()
{
	return (guyId >= 0);
}

void DiscussPane::sendMsg()
{
	moveToBottom();
	if(ui->lineEdit->text().size() > 0)
	{
		if(!isPrivate())
		{
			LutController::getInstance()->sendMsg(ui->lineEdit->text());
		}
		else
		{
			LutController::getInstance()->sendPrivateMsg(guyId,
								 ui->lineEdit->text());
		}
		displayMsg(LutController::getInstance()->myId(), ui->lineEdit->text());
	}
	ui->lineEdit->clear();
}

int DiscussPane::getGuyId()
{
	return guyId;
}

void DiscussPane::setGuyId(int val)
{
	guyId = val;
}

void DiscussPane::moveToBottom()
{
	ui->textBrowser->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
}
